package com.example.seed.interceptor;

import com.example.seed.entity.DataScopeEntity;
import com.example.seed.entity.DataScopeEntityDraft;
import com.example.seed.entity.DataScopeEntityProps;
import org.babyfish.jimmer.ImmutableObjects;
import org.babyfish.jimmer.sql.DraftInterceptor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.stereotype.Component;

/**
 * 数据范围实体草稿拦截器
 *
 * @author AAA
 * @date 2024/02/26
 */
@Component
public class DataScopeEntityDraftInterceptor implements DraftInterceptor<DataScopeEntity, DataScopeEntityDraft> {
    @Override
    public void beforeSave(@NotNull DataScopeEntityDraft draft, @Nullable DataScopeEntity original) {
        if (original == null) {
            if (!ImmutableObjects.isLoaded(draft, DataScopeEntityProps.CREATOR)) {
                draft.setCreatorId(1L);
                System.out.println("新数据填充创建人");
            }
            if (!ImmutableObjects.isLoaded(draft, DataScopeEntityProps.CREATOR_DEPT)) {
                draft.applyCreatorDept(dept -> dept.setId(1L));
                System.out.println("新数据填充创建部门");
            }
        }
    }
}
